// pages/singlePage.js
var config = require('../../config/config');
var common = require('../../utils/common');
var enum_type = require('../../utils/enum');
var util = require('../../utils/util');
var service = require('../../utils/service');

Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    //广告主跳转进来
    common.adsClickData(options);

    var that = this;
    var params = {
      appid: config.appId,
    };
    service.getSinglePage(params).then(res => {

      var title = res.title;
      wx.setNavigationBarTitle({
        title: title//页面标题为路由参数
      });
      that.setData({
        item: res
      });

      var defaultJumpType = res.defaultJumpType;
      debugger
      if (defaultJumpType == 2) {
        var appid = res.appId;
        var page = res.appUrl;
        that.navigateToMiniProgram(appid, page);
      }

    }).catch(e => {

    })
  },
  jumpClickData: function (customerAppId) {
    var page = util.getCurrentPageUrl();
    var dataPrams = {
      customerAppId: customerAppId,
      jumpType: enum_type.jumpTypeEnum.OUT
    };

    common.saveClickData(dataPrams);
  },

  jumpPage: function (event) {

    var item = event.currentTarget.dataset.item;
    var type = event.currentTarget.dataset.type;
    var dataId = item.id;
    this.jumpClickData(item.appId);

    if (item.jumpType == 1) {
      var details = item.detail;
      wx.setStorageSync('details', details);
      this.navigateToDetail();
    } else if (item.jumpType == 2) {
      this.navigateToMiniProgram(item.appId, item.appPage);
    } else if (item.jumpType == 3) {
      this.navigateToH5(item.h5Url);
    }
  },
  navigateToH5: function (h5Url) {
    wx.navigateTo({
      url: '../webview/webview?h5Url=' + h5Url,
    })
  }, navigateToDetail: function () {
    wx.navigateTo({
      url: '../detail/detail',
    })
  }, navigateToMiniProgram: function (appid, page) {
    wx.navigateToMiniProgram({
      appId: appid,
      path: page,
      envVersion: 'trial',
      success(res) {
        // 打开成功
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})